import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;

//Test
public class removefriend extends Dialog {

        protected Object result;
        protected Shell shell;
  protected ClientLogic logic;

        /**
         * Create the dialog.
         * @param parent
         * @param style
         */
        public removefriend(Shell parent, int style) {
                super(parent, style);
                setText("Remove Friend");
                logic = ClientLogic.getInstance();
        }

        /**
         * Open the dialog.
         * @return the result
         */
        public Object open() {
                createContents();
                shell.open();
                shell.layout();
                Display display = getParent().getDisplay();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();
                        }
                }
                return result;
        }

        /**
         * Create contents of the dialog.
         */
        private void createContents() {
                shell = new Shell(getParent(), getStyle());
                shell.setSize(346, 168);
                shell.setText(getText());
                
                Label lblUserName = new Label(shell, SWT.NONE);
                lblUserName.setBounds(10, 42, 85, 27);
                lblUserName.setText("User Name:");
                
                final Combo DropDown = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
                DropDown.setBounds(101, 40, 189, 29);
                HashMap<String, String> temp = logic.getFriendList();
        		ArrayList<String> listOfFriends = new ArrayList<String>(temp.keySet());
        		while (!listOfFriends.isEmpty()) {
        			DropDown.add(listOfFriends.get(0));
        			listOfFriends.remove(0);
        		}

                
                final Label resultLabel = new Label(shell, SWT.NONE);
                resultLabel.setBounds(101, 69, 233, 17);
                resultLabel.setVisible(false);
                
                Button btnAdd = new Button(shell, SWT.NONE);
                btnAdd.setBounds(10, 99, 91, 29);
                btnAdd.setText("Remove");
                btnAdd.addListener(SWT.Selection, new Listener() {
                                public void handleEvent(org.eclipse.swt.widgets.Event event) 
                                {
                                	int index = DropDown.getSelectionIndex();
                                	if (index >= 0)
                                	{
                                		String newFriend = DropDown.getItem(index);
                                		logic.removeFriend(newFriend);
                                		resultLabel.setText(newFriend + " has been removed from your Friend's List.");
                                		DropDown.removeAll();
                                        HashMap<String, String> temp = logic.getFriendList();
                                		ArrayList<String> listOfFriends = new ArrayList<String>(temp.keySet());
                                		while (!listOfFriends.isEmpty()) {
                                			DropDown.add(listOfFriends.get(0));
                                			listOfFriends.remove(0);
                                		}
                                	}
                                	else
                                	{
                                		resultLabel.setText("Please select a Friend from the list.");
                                	}
                                	resultLabel.setVisible(true);
                        }
                });
                
                Button btnCancel = new Button(shell, SWT.NONE);
                btnCancel.setBounds(243, 99, 91, 29);
                btnCancel.setText("Close");
                btnCancel.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(org.eclipse.swt.widgets.Event event) {
                                shell.close();
                        }
                });

        }
}